package com.jason.controller;

import com.jason.entities.JsonData;
import com.jason.entities.Payment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/consumer")
public class PaymentController {
//    public static final String PAYMENT_URL="http://localhost:8001"; 单机
    public static final String PAYMENT_URL="http://CLOUD-PAYMENT-SERVICE";
    @Autowired
    private RestTemplate restTemplate;
    //调用客户端的订单操作
    @PostMapping("/create")
    public JsonData create(@RequestBody Payment payment){
        System.out.println(payment.getSerial());

        return restTemplate.postForObject(PAYMENT_URL+"/api/v1/payment/inser",payment,JsonData.class);
    }
    @GetMapping("/get/{id}")
    public JsonData getPayment(@PathVariable Long id){
        return restTemplate.getForObject(PAYMENT_URL+"/api/v1/payment/list/"+id,JsonData.class);

    }
    @GetMapping("/getx/{id}")
    public JsonData getPayment2(@PathVariable Long id){
        ResponseEntity<JsonData> entity = restTemplate.getForEntity(PAYMENT_URL + "/api/v1/payment/list/" + id, JsonData.class);
        if (entity.getStatusCode().is2xxSuccessful()){
            //状态响应码 2xx
            return entity.getBody();
        }else {
            return JsonData.buildError(404,"操作失败");
        }

    }

}
